//
//  SpecialTopicViewController.m
//  好听-1
//
//  Created by luojin on 15/4/13.
//  Copyright (c) 2015年 MyCompany. All rights reserved.
//

#import "SpecialTopicViewController.h"
#import "TopicTableViewCell.h"
#import "MyConnection.h"
#import "SpecialTopicModel.h"
#import "UIImageView+WebCache.h"

#define Special @"http://mobile.ximalaya.com/m/subject_list"
#define SCREEN_X 375
#define SCREEN_Y 667

@interface SpecialTopicViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *tableArray;

@end

@implementation SpecialTopicViewController

- (void)dealloc
{
    [_tableArray release];
    [_table release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor redColor]];
    [self creatTable];
    [self getConnection];
}

- (void)creatTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_X, SCREEN_Y) style:UITableViewStyleGrouped];
//    _table.separatorStyle = NO;
    _table.rowHeight = 200;
    _table.delegate = self;
    _table.dataSource = self;
    [_table registerClass:[TopicTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table];
    [_table release];
   
}

- (void)getConnection
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"10" forKey:@"per_page"];
    
    [MyConnection connectWithUrl:Special parmaters:dic finishBlock:^(NSData *data) {
        NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [dictionary objectForKey:@"list"];
        NSDate *date = [NSDate date];
        NSMutableArray *dateArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            SpecialTopicModel *model = [[SpecialTopicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            if (![model.title containsString:@"喜马拉雅"]) {
                
                NSDate * nowDate = [NSDate dateWithTimeIntervalSince1970:model.releasedAt / 1000];
                if (date != nowDate) {
                    date = nowDate;
                    [dateArray addObject:date];
                }
                [self.tableArray addObject:model];
            } 
        }
        [self.table reloadData];
    }]; 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_tableArray count];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_X, SCREEN_Y * 0.15)];
    SpecialTopicModel *model = [self.tableArray objectAtIndex:section];
    
    NSDate * nowADate = [NSDate dateWithTimeIntervalSince1970:model.releasedAt / 1000];
    NSLog(@"%@",nowADate);
    
    titleLable.text = model.title;
    return titleLable;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    SpecialTopicModel *model = [self.tableArray objectAtIndex:indexPath.row];
    
    NSURL *url = [NSURL URLWithString:model.coverPathBig];
    [cell.showIma setImageWithURL:url];
    
    cell.topicLable.text = model.title;
    
    
    return cell;
}

@end
